package com.xiexin.web.controller.project;

import com.xiexin.common.annotation.Log;
import com.xiexin.common.core.controller.BaseController;
import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.common.core.page.TableDataInfo;
import com.xiexin.common.enums.BusinessType;
import com.xiexin.common.utils.poi.ExcelUtil;
import com.xiexin.project.domain.Item;
import com.xiexin.project.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 单品项目Controller
 * 
 * @author xiexin
 * @date 2022-03-21
 */
@RestController
@RequestMapping("/project/item")
public class ItemController extends BaseController {
    @Autowired
    private IItemService itemService;

    /**
     * 查询单品项目列表
     */
    @PreAuthorize("@ss.hasPermi('project:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(Item item) {
        startPage();
        List<Item> list = itemService.selectItemList(item);
        return getDataTable(list);
    }

    /**
     * 导出单品项目列表
     */
    @PreAuthorize("@ss.hasPermi('project:item:export')")
    @Log(title = "单品项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Item item) {
        List<Item> list = itemService.selectItemList(item);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        util.exportExcel(response, list, "单品项目数据");
    }

    /**
     * 获取单品项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(itemService.selectItemById(id));
    }

    /**
     * 新增单品项目
     */
    @PreAuthorize("@ss.hasPermi('project:item:add')")
    @Log(title = "单品项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Item item) {
        return toAjax(itemService.insertItem(item));
    }

    /**
     * 修改单品项目
     */
    @PreAuthorize("@ss.hasPermi('project:item:edit')")
    @Log(title = "单品项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Item item) {
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除单品项目
     */
    @PreAuthorize("@ss.hasPermi('project:item:remove')")
    @Log(title = "单品项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(itemService.deleteItemByIds(ids));
    }
}
